﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RssApp.Controllers
{

    /// <summary>
    /// 博客
    /// </summary>
    public class BlogController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Index(int id = 0)
        {
            var model = new Models.BlogViewModel();
            try
            {
                if (id <= 0) return Content("参数无效！");
                //Domain
                Rss.Domain.Author author = new Rss.Domain.Author();
                var theAuthor = author.GetAuthorById(id: id);
                if (null == theAuthor) throw new ArgumentOutOfRangeException("id");

                //获取博客信息
                model.author = theAuthor;
                var blogs = new List<Models.Blog>();
                model.blog = blogs;
                foreach (var rss in theAuthor.Rss)
                {
                    //解析器
                    Rss.Infrastructure.IBlogParser parser = null;
                    if (rss.Type.ToLower() == "oschina")
                    {
                        parser = new Parser.OscParser();
                    }
                    else if (rss.Type.ToLower() == "cnblogs")
                    {
                        parser = new Parser.CnblogsParser();
                    }
                    //博客
                    Models.Blog blog = new Models.Blog();
                    blog.Rss = rss;
                    blog.Blogs = rss.TryCatchBlogs(parser);
                    blogs.Add(blog);
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
            return View(model);
        }
    }
}